package com.salmon.security.core;

import com.salmon.security.core.properties.SecurityProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;

@Configuration
@EnableConfigurationProperties(SecurityProperties.class)
public class SecurityCoreConfig {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

 /*   @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }*/

    /*
     报错信息
     org.springframework.expression.spel.SpelEvaluationException: EL1057E:
     No bean resolver registered in the context to resolve access to bean 'rbacService'

     在5.0.7 以上 版本需要把OAuth2WebSecurityExpressionHandle换成DefaultWebSecurityExpressionHandler，
     可以解决这个问题，在ResourceServerConfig中添加就可以了。

     但还想用OAuth2WebSecurityExpressionHandle 这个处理 ，解决思路如下
     1、 配置一个 bean
     @Bean
     public OAuth2WebSecurityExpressionHandler oAuth2WebSecurityExpressionHandler(ApplicationContext applicationContext) {
        OAuth2WebSecurityExpressionHandler expressionHandler = new OAuth2WebSecurityExpressionHandler();
        expressionHandler.setApplicationContext(applicationContext);
        return expressionHandler;
     }
     2、 注入
     @Configuration
     @EnableResourceServer
     public class PcResourceServerConfig extends ResourceServerConfigurerAdapter {

        @Autowired
        private OAuth2WebSecurityExpressionHandler expressionHandler;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
            resources.expressionHandler(expressionHandler);
        }
     }
     */
    @Bean(name = "expressionHandler")
    public OAuth2WebSecurityExpressionHandler oAuth2WebSecurityExpressionHandler(ApplicationContext applicationContext) {
        OAuth2WebSecurityExpressionHandler expressionHandler = new OAuth2WebSecurityExpressionHandler();
        expressionHandler.setApplicationContext(applicationContext);
        return expressionHandler;
    }


}
